<?php

use Biz\model\activity\SNJActivityRecord;

/**
 * @deprecated
 * 三棱镜 广告活动记录
 * Class SNJRecordAction
 */
class SNJRecordAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;

    public function main()
    {
        $data = [];
        $data['area'] = $this->getRequest()->getPost('area');
        $data['area_name'] = $this->getRequest()->getPost('area_name');
        $data['house_size'] = $this->getRequest()->getPost('house_size');//房屋面积
        $data['floor_plan_pic_url'] = $this->getRequest()->getPost('floor_plan_pic_url');//户型图
        $data['ground_products_type'] = $this->getRequest()->getPost('ground_products_type'); // 地面产品类型
        $data['wall_products_type'] = $this->getRequest()->getPost('wall_products_type');
        $data['rock_plate_place'] = $this->getRequest()->getPost('rock_plate_place');//岩版安装地点
        // 判断是否填写房屋位置
        if (empty($data['area'])) {
            throw new \Exception('请输入房屋位置', self::CODE_ERROR_VIEW_TOSTA);
        }
        // 判断地区格式是否正确
        if (strpos($data['area'], ',')) {
            $areas = explode(',', $data['area']);
            foreach ($areas as $key => $value) {
                if (! is_numeric($value)) {
                    throw new \Exception('房屋位置输入格式错误', self::CODE_ERROR_VIEW_TOSTA);
                }
            }
        } else {
            throw new \Exception('房屋位置输入格式错误', self::CODE_ERROR_VIEW_TOSTA);
        }

        // 判断是否填写房屋位置
        if (empty($data['area_name'])) {
            throw new \Exception('请输入房屋位置', self::CODE_ERROR_VIEW_TOSTA);
        }
        if (! is_numeric($data['house_size'])) {
            throw new \Exception('房屋大小只能输入数字', self::CODE_ERROR_VIEW_TOSTA);
        }
        if(empty($data['floor_plan_pic_url'])){
            throw new \Exception('没有户型图', self::CODE_ERROR_VIEW_TOSTA);
        }

        if(empty($data['ground_products_type'])){
            throw new \Exception('没选择地面类型', self::CODE_ERROR_VIEW_TOSTA);
        }
        if(empty($data['wall_products_type'])){
            throw new \Exception('没选择墙面类型', self::CODE_ERROR_VIEW_TOSTA);
        }
        if(empty($data['rock_plate_place'])){
            throw new \Exception('没选择岩版安装地点', self::CODE_ERROR_VIEW_TOSTA);
        }

//        $this->uid = 55010;
        $inserInfo = [];
        $inserInfo['uid'] = $this->uid;
        $inserInfo['type'] = SNJActivityRecord::NORMAL_TYPE;
        $inserInfo['extend_info'] = json_encode($data);
        $info = SNJActivityRecord::getRecordByCondition(['uid' => $this->uid , 'type' => SNJActivityRecord::NORMAL_TYPE]);
        if($info){
            $inserInfo['id'] = $info['id'];
            $result = SNJActivityRecord::updateRecord($inserInfo);
        }else{
            $result =  SNJActivityRecord::inserRecord($inserInfo);
        }

        if($result){
            $this->setSuccess();
        }else{
            throw new \Exception('报名失败', self::CODE_ERROR_VIEW_TOSTA);
        }
    }
}